
package sample;

import java.awt.MenuItem;
import java.awt.PopupMenu;
import java.awt.SystemTray;
import java.awt.TrayIcon;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.image.BufferedImage;
import javafx.application.Platform;
import javafx.stage.Stage;
import javax.imageio.ImageIO;

public class TrayUtil {
    private static TrayIcon trayIcon;

    public TrayUtil() {
    }

    public static void initTray(final Stage stage, String title) {
        PopupMenu popupMenu = new PopupMenu();
        MenuItem openItem = new MenuItem("显示");
        MenuItem hideItem = new MenuItem("最小化");
        MenuItem quitItem = new MenuItem("退出");
        ActionListener acl = new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                MenuItem item = (MenuItem)e.getSource();
                Platform.setImplicitExit(false);
                if (item.getLabel().equals("退出")) {
                    SystemTray.getSystemTray().remove(TrayUtil.trayIcon);
                    Platform.exit();
                } else {
                    if (item.getLabel().equals("显示")) {
                        Platform.runLater(new Runnable() {
                            public void run() {
                                stage.show();
                            }
                        });
                    }

                    if (item.getLabel().equals("最小化")) {
                        Platform.runLater(new Runnable() {
                            public void run() {
                                stage.hide();
                            }
                        });
                    }

                }
            }
        };
        MouseListener sj = new MouseListener() {
            public void mouseReleased(MouseEvent e) {
            }

            public void mousePressed(MouseEvent e) {
            }

            public void mouseExited(MouseEvent e) {
            }

            public void mouseEntered(MouseEvent e) {
            }

            public void mouseClicked(MouseEvent e) {
                Platform.setImplicitExit(false);
                if (e.getClickCount() == 2) {
                    System.out.println(stage.isShowing());
                    if (stage.isShowing()) {
                        Platform.runLater(new Runnable() {
                            public void run() {
                                stage.hide();
                            }
                        });
                    } else {
                        Platform.runLater(new Runnable() {
                            public void run() {
                                stage.show();
                            }
                        });
                    }
                }

            }
        };
        openItem.addActionListener(acl);
        quitItem.addActionListener(acl);
        hideItem.addActionListener(acl);
        popupMenu.add(openItem);
        popupMenu.add(hideItem);
        popupMenu.add(quitItem);

        try {
            SystemTray tray = SystemTray.getSystemTray();
            BufferedImage image = ImageIO.read(Main.class.getResourceAsStream("/resourse/tray.png"));
            trayIcon = new TrayIcon(image, title, popupMenu);
            trayIcon.setToolTip(title);
            trayIcon.addMouseListener(sj);
            tray.add(trayIcon);
        } catch (Exception var10) {
            var10.printStackTrace();
        }

    }

    public static TrayIcon getTrayIcon() {
        return trayIcon;
    }
}